package com.ajax.servlet; /**
 * @author wzm
 * @create 2023-03-23 10:42
 */

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/getAll")
public class SelectByStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        // 处理乱码的时候考虑相应json数据
        response.setContentType("text/json;charset=utf-8");

        //模拟调用Service查询全部返回对象
       // List<Student> list = studentService.getList();
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,"张三1","123456"));
        list.add(new Student(2,"张三2","123456"));
        list.add(new Student(3,"张三3","123456"));
        list.add(new Student(4,"张三4","123456"));
        list.add(new Student(5,"张三5","123456"));

        //将Java对象转为JSON字符串用来传输
        String s = JSON.toJSONString(list);
        //将字符串相应给浏览器
        response.getWriter().write(s);
    }
}
